package com.tencreat.service.mall.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.domain.mall.MallSetmealNorms;
import com.tencreat.mapper.mall.MallSetmealNormsMapper;
import com.tencreat.service.mall.IMallSetmealNormsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 商品规格参数Service业务层处理
 *
 * @author tencreat
 * @date 2022-01-08
 */
@Service
public class MallSetmealNormsServiceImpl extends ServiceImpl<MallSetmealNormsMapper, MallSetmealNorms> implements IMallSetmealNormsService {


    @Autowired
    MallSetmealNormsMapper setmealNormsMapper;
    /**
     * 查询商品规格参数
     *
     * @param id 商品规格参数ID
     * @return 商品规格参数
     */
    @Override
    public MallSetmealNorms selectMallSetmealNormsById(ReqIdParam id) {
        return baseMapper.selectById(id.getId());
    }

    /**
     * 查询商品规格参数列表
     *
     * @param mallSetmealNorms 商品规格参数
     * @return 商品规格参数
     */
    @Override
    public List<MallSetmealNorms> selectMallSetmealNormsList(MallSetmealNorms mallSetmealNorms) {
        return baseMapper.selectList(Wrappers.query(mallSetmealNorms));
    }

    /**
     * 新增商品规格参数
     *
     * @param mallSetmealNorms 商品规格参数
     * @return 结果
     */
    @Override
    public int insertMallSetmealNorms(MallSetmealNorms mallSetmealNorms) {
       int sort =  setmealNormsMapper.getNewSortOrder();
        mallSetmealNorms.setCreateTime(LocalDateTime.now());
        mallSetmealNorms.setSortOrder(sort);
        return baseMapper.insert(mallSetmealNorms);
    }

    /**
     * 修改商品规格参数
     *
     * @param mallSetmealNorms 商品规格参数
     * @return 结果
     */
    @Override
    public int updateMallSetmealNorms(MallSetmealNorms mallSetmealNorms) {
        mallSetmealNorms.setUpdateTime(LocalDateTime.now());
        return baseMapper.updateById(mallSetmealNorms);
    }

    /**
     * 批量删除商品规格参数
     *
     * @param ids 需要删除的商品规格参数ID
     * @return 结果
     */
    @Override
    public int deleteMallSetmealNormsByIds(ReqIdsParam ids) {
        return baseMapper.deleteBatchIds(ids.getId());
    }

    /**
     * 删除商品规格参数信息
     *
     * @param id 商品规格参数ID
     * @return 结果
     */
    @Override
    public int deleteMallSetmealNormsById(ReqIdParam id) {
        return baseMapper.deleteById(id.getId());
    }

    @Override
    public Long getFirstNormsId(Long goodsId) {
        QueryWrapper<MallSetmealNorms> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id",goodsId);
        wrapper.orderByAsc("sort_order");
        wrapper.last(" limit 1");
        return baseMapper.selectOne(wrapper).getId();
    }
    @Override
    public MallSetmealNorms getFirstNorms(Long goodsId) {
        QueryWrapper<MallSetmealNorms> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id",goodsId);
        wrapper.orderByAsc("sort_order");
        wrapper.last(" limit 1");
        return baseMapper.selectOne(wrapper);
    }
    @Override
    public int reSortOrderReduce(MallSetmealNorms mallSetmealNorms) {
        //查出上一条数据的ID
        Long sourceId = setmealNormsMapper.getUpId(mallSetmealNorms.getSortOrder(),mallSetmealNorms.getGoodsId());
        //两个ID替换
        setmealNormsMapper.exchange(mallSetmealNorms.getId(),sourceId);
        return 0;
    }

    @Override
    public int reSortOrderPlus(MallSetmealNorms mallSetmealNorms) {
        //查出下一条数据的ID
        Long sourceId = setmealNormsMapper.getDownId(mallSetmealNorms.getSortOrder(),mallSetmealNorms.getGoodsId());
        //两个ID替换
        setmealNormsMapper.exchange(mallSetmealNorms.getId(),sourceId);
        return 0;
    }
}
